package com.itheima.aop;

import com.itheima.mapper.LogMapper;
import com.itheima.pojo.Log;
import com.itheima.pojo.User;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.SourceLocation;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;

/**
 * @Name ProcessDataService
 * @Author BaiXin
 * @Date 2022-10-21 19:50
 */
@Component
@Aspect
public class ProcessDataService {

    @Autowired
    private LogMapper logMapper;

    @Pointcut("execution(* com.itheima.service.impl.UserServiceImpl.login(..))")
    public void pt() {
    }
    @Around("pt()")
    public Object processData(ProceedingJoinPoint pjp)  {
        //获取参数
        Object[] args = pjp.getArgs();
        pjp.getSourceLocation();
        Log log = new Log();
        String name = "";
        if (args != null && args.length>0) {
            for (int i = 0; i < args.length; i++) {
                //判断是否需要处理
                if (args[i] instanceof User) {
                    //处理参数
                   name = ((User) args[i]).getName();
                }
            }
        }
        //调用方法时，需要把处理后的参数给传递出去
        Object result = null;
        try {
            result = pjp.proceed(args);
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            log.setType("登录");
            log.setOperatorName(name);
          log.setOperatorTime(new Date());
            logMapper.save(log);
        }
        return result;

    }


}
